package com.huibo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huibo.algorithm.Store;
import com.huibo.algorithm.Stuff;
import com.huibo.algorithm.StuffOperator;
import com.huibo.common.ChangeEmployee;
import com.huibo.common.WorkForm;
import com.huibo.entity.Company;
import com.huibo.entity.Employee;
import com.huibo.service.CompanyService;
import com.huibo.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class XmzTest02 {

    @Autowired
    private CompanyService companyService;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Test
    void function(){
        HashMap<String, Stuff> stuff_arr = StuffOperator.stuff_arrInit();//要init()


        List<Employee> employeeList = new ArrayList<>();
        employeeList.add(new Employee(1l, "李1", "123456", "6789@163.com", "15636563698", "1", "员工", 1, 1,"日工作时间偏好 7小时"));
        employeeList.add(new Employee(2l, "李2", "123456", "6789@163.com", "15636563698", "1", "员工", 1, 1,"工作日偏好 1、3、4、5、6"));
        employeeList.add(new Employee(3l, "李3", "123456", "6789@163.com", "15636563698", "1", "员工", 1, 1,"无偏好，正常工作"));
        employeeList.add(new Employee(4l, "李4", "123456", "6789@163.com", "15636563698", "1", "员工", 1, 1,"无偏好，正常工作"));
        employeeList.add(new Employee(5l, "李5", "123456", "6789@163.com", "15636563698", "1", "员工", 1, 1,"无偏好，正常工作"));
        employeeList.add(new Employee(6l, "李6", "123456", "6789@163.com", "15636563698", "1", "员工", 1, 1,"工作日偏好 1、3、4、5、6"));
        employeeList.add(new Employee(7l, "李7", "123456", "6789@163.com", "15636563698", "1", "员工", 1, 1,"工作日偏好 1、2、5、6"));
        employeeList.add(new Employee(8l, "李8", "123456", "6789@163.com", "15636563698", "1", "员工", 1, 1,"无偏好，正常工作"));
        employeeList.add(new Employee(9l, "李9", "123456", "6789@163.com", "15636563698", "1", "员工", 1, 1,"无偏好，正常工作"));
        employeeList.add(new Employee(10l, "李10", "123456", "6789@163.com", "15636563698", "1", "员工", 1, 1,"无偏好，正常工作"));
        employeeList.add(new Employee(11l, "李11", "123456", "6789@163.com", "15636563698", "1", "员工", 1, 1,"无偏好，正常工作"));
        employeeList.add(new Employee(12l, "李12", "123456", "6789@163.com", "15636563698", "1", "员工", 1, 1,"无偏好，正常工作"));
        employeeList.add(new Employee(13l, "李13", "123456", "6789@163.com", "15636563698", "1", "员工", 1, 1,"工作时间偏好 10:30-21:00"));
        employeeList.add(new Employee(14l, "李14", "123456", "6789@163.com", "15636563698", "1", "员工", 1, 1,"工作时间偏好 12:00-19:30"));
        employeeList.add(new Employee(15l, "李15", "123456", "6789@163.com", "15636563698", "1", "员工", 1, 1,"无偏好，正常工作"));

        for(int i = 0;i<employeeList.size();i++){
            Employee employee = employeeList.get(i);
            Stuff temp = new Stuff(employee.getId(),employee.getName(), employee.getPos(), employee.getPhone(), employee.getMail(), employee.getMarketId(), employee.getLoves());
            stuff_arr.put(temp.getName(),temp);
        }
//        Stuff a = new Stuff("张三", "店长", "173028", "qq.com", "1", "日工作时间偏好 7小时");
//        Stuff b = new Stuff("李四", "经理", "173029", "weixin.com", "1", "工作日偏好 1、3、4、5、6");
//        Stuff c = new Stuff("王五", "经理", "173029", "weixin.com", "1", "无偏好，正常工作");
//        Stuff d = new Stuff("老六", "经理", "173029", "weixin.com", "1", "无偏好，正常工作");
//        Stuff e = new Stuff("田七", "经理", "173029", "weixin.com", "1", "无偏好，正常工作");
//        Stuff f = new Stuff("丈八", "经理", "173029", "weixin.com", "1", "工作日偏好 1、3、4、5、6");
//        Stuff g = new Stuff("小松", "经理", "173029", "weixin.com", "1", "工作日偏好 1、2、5、6");
//        Stuff h = new Stuff("小美", "经理", "173029", "weixin.com", "1", "无偏好，正常工作");
//        Stuff i = new Stuff("金发妹", "经理", "173029", "weixin.com", "1", "无偏好，正常工作");
//        Stuff j = new Stuff("哈哈", "经理", "173029", "weixin.com", "1", "无偏好，正常工作");
//        Stuff k = new Stuff("12", "经理", "173029", "weixin.com", "1", "无偏好，正常工作");
//        Stuff l = new Stuff("23", "经理", "173029", "weixin.com", "1", "无偏好，正常工作");
//        Stuff m = new Stuff("34", "经理", "173029", "weixin.com", "1", "工作时间偏好 10:30-21:00");
//        Stuff n = new Stuff("45", "经理", "173029", "weixin.com", "1", "工作时间偏好 12:00-19:30");
//        Stuff o = new Stuff("56", "经理", "173029", "weixin.com", "1", "无偏好，正常工作");
//
//        stuff_arr.put(a.getName(),a);
//        stuff_arr.put(b.getName(),b);
//        stuff_arr.put(c.getName(),c);
//        stuff_arr.put(d.getName(),d);
//        stuff_arr.put(e.getName(),e);
//        stuff_arr.put(f.getName(),f);
//        stuff_arr.put(g.getName(),g);
//        stuff_arr.put(h.getName(),h);
//        stuff_arr.put(i.getName(),i);
//        stuff_arr.put(j.getName(),j);
//        stuff_arr.put(k.getName(),k);
//        stuff_arr.put(l.getName(),l);
//        stuff_arr.put(m.getName(),m);
//        stuff_arr.put(n.getName(),n);
//        stuff_arr.put(o.getName(),o);
        //店集合
        Company company = new Company();
        Store store1 = new Store(company.getName(),company.getAddress(),company.getSize(),stuff_arr);

//        Store store1 = new Store("1","张家界",100.0,stuff_arr);

        long[] employeeNum = new long[stuff_arr.size()]; //存放的是员工id的顺序
        List<List<Integer>> list = store1.decode(1,employeeNum); //存放的是每个员工一天的工作是否

        for (int i = 0;i<list.size();i++){
            System.out.print("员工" + employeeNum[i] + " ");
            for(int y = 0;y<list.get(i).size();y++){
                System.out.print(list.get(i).get(y) + " ");
            }
            System.out.println();
        }
    }


    /**
     * 数据库测试
     */
    @Test
    void function02(){
        Company byId = companyService.getById(1);
        System.out.println(byId.toString());

        Employee byId1 = employeeService.getById(3);
        System.out.println(byId1.toString());
    }

    /**
     * 将function中的步骤与数据库对接测试
     */
    @Test
    void function03(){
        HashMap<String, Stuff> stuff_arr = StuffOperator.stuff_arrInit();

        List<Employee> employeeList = new ArrayList<>();
        employeeList = employeeService.list();
        for(int i = 0;i<employeeList.size();i++){
            Employee employee = employeeList.get(i);
            Stuff temp = new Stuff(employee.getId(),employee.getName(), employee.getPos(), employee.getPhone(), employee.getMail(), employee.getMarketId(), employee.getLoves());
            stuff_arr.put(temp.getName(),temp);
        }
        Company company = companyService.getById(1);
        //这里只是把基本的信息传给了从company传给了store,里面一些重要的参数比如preWorkTime
        Store store1 = new Store(company.getName(),company.getAddress(),company.getSize(),stuff_arr);

        //下面这段代码又臭又长，是为了当company中这些参数和store1中不一样时，做个校验
        //后面我们可以修改company的这些属性来达到实现固定和自定义规则
        store1.setPreWorkTime(company.getPreWorkTime());
        store1.setStoreSizeNeedBefore(company.getStoreSizeNeedBefore());
        store1.setPassengerFlowNeed(company.getPassengerFlowNeed());
        store1.setFree_population(company.getFreePopulation());
        store1.setAftWorkTime(company.getAftWorkTime());
        store1.setStoreSizeNeedAfter1(company.getStoreSizeNeedAfter1());
        store1.setStoreSizeNeedAfter2(company.getStoreSizeNeedAfter2());

        long[] employeeNum = new long[stuff_arr.size()]; //存放的是员工id的顺序
        List<List<Integer>> list = store1.decode(1,employeeNum); //存放的是每个员工一天的工作是否

        for (int i = 0;i<list.size();i++){
            System.out.print("员工" + employeeNum[i] + " ");
            for(int y = 0;y<list.get(i).size();y++){
                System.out.print(list.get(i).get(y) + " ");
            }
            System.out.println();
        }
    }

    @Test
    void function04(){
        String marketId = "1";
        Long id = 1L;
        int day = 1;
        //用来存放算法中的员工
        HashMap<String, Stuff> stuff_arr = StuffOperator.stuff_arrInit();
        //存放对应店铺的员工集
        List<Employee> employeeList = new ArrayList<>();

        //构造查询条件
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(marketId),Employee::getMarketId,marketId);

        //查询
        employeeList = employeeService.list(queryWrapper);


        //将查询出来的员工放入到map集合中
        for(int i = 0;i<employeeList.size();i++){
            Employee employee = employeeList.get(i);
            Stuff temp = new Stuff(employee.getId(),employee.getName(), employee.getPos(), employee.getPhone(), employee.getMail(), employee.getMarketId(), employee.getLoves());
            stuff_arr.put(temp.getName(),temp);
        }

        //查询到对应店铺
        Company company = companyService.getById(id);

        //这里只是把基本的信息传给了从company传给了store,里面一些重要的参数比如preWorkTime
        Store store1 = new Store(company.getName(),company.getAddress(),company.getSize(),stuff_arr);
        //下面这段代码又臭又长，是为了当company中这些参数和store1中不一样时，做个校验
        //后面我们可以修改company的这些属性来达到实现固定和自定义规则
        store1.setPreWorkTime(company.getPreWorkTime());
        store1.setStoreSizeNeedBefore(company.getStoreSizeNeedBefore());
        store1.setPassengerFlowNeed(company.getPassengerFlowNeed());
        store1.setFree_population(company.getFreePopulation());
        store1.setAftWorkTime(company.getAftWorkTime());
        store1.setStoreSizeNeedAfter1(company.getStoreSizeNeedAfter1());
        store1.setStoreSizeNeedAfter2(company.getStoreSizeNeedAfter2());

        long[] employeeNum = new long[stuff_arr.size()]; //存放的是员工id的顺序
        List<Map<String,List<Integer>>> out = new ArrayList<>();
//        Map<Long,List<Integer>> res1 = new HashMap<>();
//        Map<Long,List<Integer>> res2 = new HashMap<>();
//        Map<Long,List<Integer>> res3 = new HashMap<>();
//        Map<Long,List<Integer>> res4 = new HashMap<>();
//        Map<Long,List<Integer>> res5 = new HashMap<>();
//        Map<Long,List<Integer>> res6 = new HashMap<>();
//        Map<Long,List<Integer>> res7 = new HashMap<>();

        for(int i = 1;i<=7;i++){
            List<List<Integer>> list = store1.decode(i,employeeNum); //存放的是每个员工一天的工作是否
            Map<String,List<Integer>> res = new HashMap<>();
            for(int j = 0;j<list.size();j++){
                res.put(String.valueOf(employeeNum[j]),list.get(j));
            }
            out.add(res);
        }

//        for (int i = 0;i<list.size();i++){
//            System.out.print("员工" + employeeNum[i] + " ");
//            for(int y = 0;y<list.get(i).size();y++){
//                System.out.print(list.get(i).get(y) + " ");
//            }
//            System.out.println();
//        }

        WorkForm workForm = new WorkForm(out.get(0),out.get(1),out.get(2),out.get(3),out.get(4),out.get(5),out.get(6));
        String string = JSON.toJSONString(workForm);
        redisTemplate.opsForValue().set("data",string,120,TimeUnit.SECONDS);
        String s = redisTemplate.opsForValue().get("data");
        WorkForm dd = JSON.parseObject(s,new TypeReference<WorkForm>(){});
        System.out.println(dd.toString());
    }

    @Test
    void function05(){
        Employee employee = new Employee(1l, "李1", "123456", "6789@163.com", "15636563698", "1", "员工", 1, 1,"日工作时间偏好 7小时");
        String string = JSON.toJSONString(employee);
        redisTemplate.opsForValue().set("1",string,120,TimeUnit.SECONDS);
        Employee dd = JSON.parseObject(string,new TypeReference<Employee>(){});

        System.out.println(dd.getClass());
    }

    @Test
    void function06(){
//        Employee employee = new Employee(1l, "李1", "123456", "6789@163.com", "15636563698", "1", "员工", 1, 1,"日工作时间偏好 7小时");
//        String string = JSON.toJSONString(employee);
//        redisTemplate.opsForValue().set("1",string,100,TimeUnit.MILLISECONDS);

//        Date today = new Date();
//        Calendar c=Calendar.getInstance();
//        c.setTime(today);
//        int weekday=c.get(Calendar.DAY_OF_WEEK);
//        System.out.println(weekday);

//        Employee employee1 = new Employee(1l, "李1", "123456", "6789@163.com", "15636563698", "1", "员工", 1, 1,"日工作时间偏好 7小时");
//        Employee employee2 = new Employee(2l, "李2", "123456", "6789@163.com", "15636563698", "1", "员工", 1, 1,"工作日偏好 1、3、4、5、6");
//        ChangeEmployee changeEmployee = new ChangeEmployee(employee1,employee2,1,2,2,2);
//        String string = JSON.toJSONString(changeEmployee);
//        System.out.println(string);
    }

    @Test
    void function07(){
        Employee employee = new Employee(3l, "孙3", "123456", "6789@163.com", "15636563698", "1", "员工", 1, 1,"日工作时间偏好 7小时");
        //查询用户名和密码
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getName,employee.getName()).eq(Employee::getPassword,employee.getPassword());
        Employee one = employeeService.getOne(lambdaQueryWrapper);
        if(one!=null){
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }
    }
}
